﻿using AutoMapper;
using FluentValidation;
using Mes.Application.DTO.Inputs.Operations;
using Mes.Application.Services.Operations;
using Mes.Application.Common;
using Microsoft.AspNetCore.Mvc;

namespace Mes.Application.Web.Controllers.Operations
{
    [ApiController]
    [Route("/api/v1")]
    public class AttrController : ControllerBase
    {
        private readonly ILogger<AttrController> _logger;
        private readonly IAttrService _attrService;
        private readonly IMapper _mapper;
        private readonly IValidator<AttrInput> _attrInputValidator;
        private readonly IValidator<AttrUpdateInput> _attrUpdateInputValidator;
        private readonly IValidator<AttrPageInput> _attrPageInputValidator;

        public AttrController(ILogger<AttrController> logger,
            IAttrService attrService,
            IMapper mapper,
            IValidator<AttrInput> attrInputValidator,
            IValidator<AttrUpdateInput> attrUpdateInputValidator,
            IValidator<AttrPageInput> attrPageInputValidator
            )
        {
            _logger = logger;
            _attrService = attrService;
            _mapper = mapper;
            _attrInputValidator = attrInputValidator;
            _attrUpdateInputValidator = attrUpdateInputValidator;
            _attrPageInputValidator = attrPageInputValidator;
        }

        /// <summary>
        /// 新增属性
        /// </summary>
        /// <param name="attrInput"></param>
        /// <returns></returns>
        [HttpPost("attr")]
        public async Task<ActionResult> AddAttr(AttrInput attrInput)
        {
            try
            {
                var input = await _attrInputValidator.ValidateAsync(attrInput);
                if (!input.IsValid)
                {
                    return BadRequest(input.Errors.First().ErrorMessage);
                }

                await _attrService.AddAttributeAsync(attrInput);
                return Ok();
            }
            catch (CustomException ex)
            {
                _logger.LogError(ex.Message);
                return BadRequest(ex.Message);
            }
        }

        /// <summary>
        /// 编辑属性
        /// </summary>
        /// <param name="attrUpdateInput"></param>
        /// <returns></returns>
        [HttpPut("attr")]
        public async Task<ActionResult> UpdateAttr(AttrUpdateInput attrUpdateInput)
        {
            try
            {
                var input = await _attrUpdateInputValidator.ValidateAsync(attrUpdateInput);
                if (!input.IsValid)
                {
                    return BadRequest(input.Errors.First().ErrorMessage);
                }

                await _attrService.UpdateAttributeAsync(attrUpdateInput);
                return Ok();
            }
            catch (CustomException ex)
            {
                _logger.LogError(ex.Message);
                return BadRequest(ex.Message);
            }
        }

        /// <summary>
        /// 删除属性
        /// </summary>
        /// <param name="attr_ids"></param>
        /// <returns></returns>
        [HttpDelete("attr")]
        public async Task<ActionResult> DeleteAttr(int[] attr_ids)
        {
            try
            {
                await _attrService.DeleteAttributeAsync(attr_ids);
                return Ok();
            }
            catch (CustomException ex)
            {
                _logger.LogError(ex.Message);
                return BadRequest(ex.Message);
            }
        }

        /// <summary>
        /// 分页查询属性
        /// </summary>
        /// <param name="attrPageInput"></param>
        /// <returns>PagedItemsData<AttrResult></returns>
        [HttpGet("attr")]
        public async Task<ActionResult> GetAttr([FromQuery] AttrPageInput attrPageInput)
        {
            try
            {
                var input = await _attrPageInputValidator.ValidateAsync(attrPageInput);
                if (!input.IsValid)
                {
                    return BadRequest(input.Errors.First().ErrorMessage);
                }

                var result = await _attrService.GetAttributesAsync(attrPageInput);
                return Ok(result);
            }
            catch (CustomException ex)
            {
                _logger.LogError(ex.Message);
                return BadRequest(ex.Message);
            }
        }

        /// <summary>
        /// 查询属性
        /// </summary>
        /// <param name="attr_id"></param>
        /// <returns>AttrResult</returns>
        [HttpGet("attr/entity")]
        public async Task<ActionResult> GetAttrEntityById(int attr_id)
        {
            try
            {
                var result = await _attrService.GetAttributeAsync(attr_id);
                return Ok(result);
            }
            catch (CustomException ex)
            {
                _logger.LogError(ex.Message);
                return BadRequest(ex.Message);
            }
        }
    }
}
